#pragma once

#include "layer_ref.h"

namespace qbe::LayerRef {
void exit(ecs::Query query, state::AllModeType enter_mode){
}

void init(entt::registry& reg, ecs::Commands commands, ecs::Query query){
}

//进入下层业务
void update_enter_ref_cell(entt::registry& ecs_reg, ecs::Query query, ecs::Bus& bus){
  auto& state = query.get_resource<state::State>();
  // 仅在模式执行
  if (state.getEditingMode() != state::EditingModeType::EnterRefCell)
    return;
  state.switchMode(state::EditingModeType::None);

  auto& cmd = query.get_resource<layout::CellCmd>();
  auto cell = cmd.get_cell();
  auto& reg = cell->reg;
  auto& cell_state = query.get_resource<layout::CellState>();
  auto& axis_t = query.get_resource<axis::Axis>();
  auto& param = query.get_resource<LayerModel>();
  auto& hand = query.get_resource<vsg::ref_ptr<input::HandleBundle>>();
    // auto ref_ptr = reg.try_get<qlib::Reference::s_ptr>(param.select_ref);  //选中的引用
  // //to do... 渲染引用器件
  // param.level_cells.push_back(cell);  //记录当前层级
  std::vector<qlib::Reference::s_ptr> checked_ref_ptrs;
  qlib::Reference::s_ptr checked_ref_ptr;

  // 遍历选中对象
  auto view = reg.view<select::SelectTag>();
  bool other_item = false;
  entt::entity ref_entt;
  view.each([&reg, &checked_ref_ptrs, &other_item, &ref_entt](entt::entity e) {
    auto checked_ref = reg.try_get<qlib::Reference::s_ptr>(e);
    if(checked_ref){
        checked_ref_ptrs.push_back(*checked_ref);
        ref_entt = e;
    }else{
      other_item = true;
    }
  });
  
  if (other_item || checked_ref_ptrs.empty() || checked_ref_ptrs.size() >1 || param.editing_ref_cell) {
    return;
  }
  checked_ref_ptr =  checked_ref_ptrs[0];
  if (checked_ref_ptr) {
    reg.remove<select::SelectTag>(ref_entt);
    auto& sele_event_writer = query.get_event_writer<select::Event>();
    sele_event_writer.send({select::Code::SelectUpdate});
    sele_event_writer.send({select::Code::HoverUpdate});
    qbe::UiMessage::SwitchCell_OP switch_data;
    switch_data.cell = checked_ref_ptr->cell;
    bus.send(switch_data);
  }
}

//返回上层业务
void update_back_upper_layer(entt::registry& ecs_reg, ecs::Query query, ecs::Bus& bus) {
  auto& state = query.get_resource<state::State>();
  // 仅在模式执行
  if (state.getEditingMode() != state::EditingModeType::BackUpperLayer)
    return;
  
  state.switchMode(state::EditingModeType::None);
  auto& cmd = query.get_resource<layout::CellCmd>();
  auto cell = cmd.get_cell();
  auto& reg = cell->reg;
  auto& cell_state = query.get_resource<layout::CellState>();
  auto& axis_t = query.get_resource<axis::Axis>();
  auto& param = query.get_resource<LayerModel>();
  auto& hand = query.get_resource<vsg::ref_ptr<input::HandleBundle>>();

  if (param.editing_ref_cell) {
    param.editing_ref_cell = false;  //退出编辑下层状态
    //to do... 切换编辑器件
  } else {
    // auto to_cell = param.level_cells[param.level_cells.size() - 1];  //返回到的器件
    // //to do... 渲染器件
    // param.level_cells.pop_back();
    qbe::UiMessage::ReturnUpperCell_OP op;
    bus.send(op);
  }
}

//返回最上层业务
void update_back_top_layer(entt::registry& ecs_reg, ecs::Query query, ecs::Bus& bus) {
  auto& state = query.get_resource<state::State>();
  // 仅在模式执行
  if (state.getEditingMode() != state::EditingModeType::BackTopLayer)
    return;
  
  state.switchMode(state::EditingModeType::None);
  auto& cmd = query.get_resource<layout::CellCmd>();
  auto cell = cmd.get_cell();
  auto& reg = cell->reg;
  auto& cell_state = query.get_resource<layout::CellState>();
  auto& axis_t = query.get_resource<axis::Axis>();
  auto& param = query.get_resource<LayerModel>();
  auto& hand = query.get_resource<vsg::ref_ptr<input::HandleBundle>>();

  if (param.editing_ref_cell) {
    param.editing_ref_cell = false;  //退出编辑下层状态
    //to do... 切换编辑器件
  } else {
    // auto to_cell = param.level_cells[0];  //返回到的器件
    // //to do... 渲染器件
    // param.level_cells.clear();
    qbe::UiMessage::ReturnTopCell_OP op;
    bus.send(op);
  }
}


void post(entt::registry& reg, ecs::Commands commands, ecs::Query query, ecs::Bus& bus){

}

void LayerRefPlugin::build(ecs::App& app){
  app.init_resource<LayerModel>(LayerModel{});

  app.add_systems<ecs::stage::Startup>(init);
  app.add_systems<ecs::stage::Update>(update_enter_ref_cell);
  app.add_systems<ecs::stage::Update>(update_back_upper_layer);
  app.add_systems<ecs::stage::Update>(update_back_top_layer);
  app.add_systems<ecs::stage::PostUpdate>(post);
};
}